package org.minbox.framework.logging.admin;

import org.minbox.framework.logging.admin.listener.ReportLogJsonFormatListener;
import org.minbox.framework.logging.admin.listener.ReportLogStorageListener;
import org.minbox.framework.logging.admin.storage.LoggingDataSourceStorage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.Assert;

import javax.sql.DataSource;
import javax.validation.constraints.NotNull;

/**
 * MinBox Logging Admin FactoryBean
 * Provide the parameter configuration needed by the management side
 * <p>
 * Register the following beans to BeanFactory{@link org.springframework.beans.factory.BeanFactory}：
 * Register {@link LoggingDataSourceStorage} bean
 * Register {@link ReportLogStorageListener} bean
 * Register {@link ReportLogJsonFormatListener} bean
 * Register {@link org.minbox.framework.logging.admin.endpoint.LoggingEndpoint} bean
 *
 * @author 恒宇少年
 * @since 1.0.1
 */
public class LoggingAdminFactoryBean
        implements ApplicationContextAware {
    /**
     * logger instance
     */
    static Logger logger = LoggerFactory.getLogger(LoggingAdminFactoryBean.class);
    /**
     * Database connection pool
     */
    @NotNull
    private DataSource dataSource;
    /**
     * Whether to display the logs reported on the console
     */
    private boolean showConsoleReportLog;
    /**
     * Does the console display the formatted log JSON
     */
    private boolean formatConsoleLogJson;
    /**
     * spring {@link ApplicationContext} application context
     */
    private ApplicationContext applicationContext;

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        Assert.notNull(dataSource, "DataSource Can't not be null.");
        this.dataSource = dataSource;
    }

    public boolean isShowConsoleReportLog() {
        return showConsoleReportLog;
    }

    public void setShowConsoleReportLog(boolean showConsoleReportLog) {
        this.showConsoleReportLog = showConsoleReportLog;
    }

    public boolean isFormatConsoleLogJson() {
        return formatConsoleLogJson;
    }

    public void setFormatConsoleLogJson(boolean formatConsoleLogJson) {
        this.formatConsoleLogJson = formatConsoleLogJson;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        logger.debug("ApplicationContext set complete.");
    }

}
